<?php
    namespace app\service;

use api\utils\APIClient;
use api\utils\MpeApiClient;
use api\utils\PayApiClient;
use app\service\interfaces\IMpeService;
use common\service\CommonService;
use herosphp\bean\Beans;
use herosphp\core\Loader;
use herosphp\core\Log;
use herosphp\string\StringUtils;
use payment\models\SignVo;
use payment\utils\SerialNumberUtils;
use herosphp\utils\ArrayUtils;

Loader::import('app.service.interfaces.IMpeService');

/**
 * app(Service)接口实现
 * @package app\service
 * @author yangjian<yangjian102621@gmail.com>
 */
class MpeService extends CommonService implements IMpeService {

    /**
     * 开通MPE
     * @param array $data
     * @return bool
     */
    public function add($data)
    {
        $userid = $data['userid'];
        if ( empty($userid) ) return false;
        //1. 向支付系统发起签约请求，生成结算户
        $install_id = StringUtils::genGlobalUid(true, 24);
        $resVo = PayApiClient::send('api/appStatAccount/sign', array('appid' => $data['appid'], 'install_id' => $install_id,  'userid' => $userid));
        if ( $resVo->isSucess() ) {
            //添加附属帐号信息
            $item = $resVo->getItem();
            $data['virtual_name'] = $item['virtual_name'];
            $data['virtual_account'] = $item['virtual_account'];

            $this->beginTransaction(); //开启事务
            $data['id'] = $install_id;
            if ( $this->getModelDao()->add($data) ) {
                //1. 更新应用的安装次数
                $appService = Beans::get('app.app.service');
                $appService->increase('install_num', 1, $data['appid']);

                //2. 调用MPE远程api, 开通MPE
                $data['mpeid'] = $install_id;
                $res = MpeApiClient::send('api/mpe/create', array("data" => $data));
                if ( $res->isSucess() ) {
                    $this->commit();
                    return true;
                } else {
                    $this->rollback();
                    Log::info("MPE端开通失败，".$res->getMessage());
                    return false;
                }
            }

        } else {
            Log::info("创建结算户失败， {$resVo->getMessage()}");
        }

        return false;

    }


    public function update($data, $id)
    {
        $this->beginTransaction();
        $result = $this->getModelDao()->update($data, $id);
        if ( $result != false && (isset($data['domain']) || isset($data['level2_domain'])) ) {
            //2. 调用远程api
            $params['mpeid'] = $id;
            $params['data'] = $data;
            $res = MpeApiClient::send('api/mpe/update', $params);
            if ( $res->isSucess() ) {
                $this->commit();
                return true;
            } else {
                $this->rollback();
                Log::info($res->getMessage());
                return false;
            }
        }
        return $result;
    }

    /**
     * 启用MPE
     * @param $mpeid
     * @return mixed
     */
    public function start($mpeid)
    {

        $this->beginTransaction();
        $result = $this->set('status', 1, $mpeid);
        if ( $result != false ) {
            //2. 调用远程api
            $params['mpeid'] = $mpeid;
            $res = MpeApiClient::send('api/mpe/start', $params);
            if ( $res->isSucess() ) {
                $this->commit();
                return true;
            } else {
                $this->rollback();
                Log::info($res->getMessage());
                return false;
            }
        }
        return $result;
    }

    /**
     * 关闭MPE
     * @param $mpeid
     * @return mixed
     */
    public function stop($mpeid)
    {
        $this->beginTransaction();
        $result = $this->set('status', 0, $mpeid);
        if ( $result != false ) {
            //2. 调用远程api
            $params['mpeid'] = $mpeid;
            $res = MpeApiClient::send('api/mpe/stop', $params);
            if ( $res->isSucess() ) {
                $this->commit();
                return true;
            } else {
                $this->rollback();
                Log::info($res->getMessage());
                return false;
            }
        }
        return $result;
    }
}
